package com.sofwin.configer;

import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * @packageName: com.sofwin.configer
 * @user: andyliu
 * @date: 2022/12/16 9:57
 * @email 115176513@qq.com
 * @description: 替换web.xml
 */

public class WebConfiger extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 获取普通的applicationContext.xml中的配置
     * applicationContext.xml会被替换成java类
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{Configer.class};
    }

    /**
     * spring mvc
     * applicationContext-mvc.xml 替换成Java类
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{MvcConfiger.class};
    }

    /**
     * 设置web.xml中前端控制器 url-pattern
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("utf-8");
        characterEncodingFilter.setForceEncoding(true);
        servletContext.addFilter("encodingFilter",characterEncodingFilter).addMappingForUrlPatterns(null,true,"/*");
        DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
        servletContext.addFilter("shiroFilter",delegatingFilterProxy).addMappingForUrlPatterns(null,true,"/*");
    }
}
